package com.qifan.alb2b.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;

import com.cqcis.base.orm.jdbc.JdbcUtil;
import com.qifan.alb2b.domain.AdminInfoDomain;
import com.qifan.alb2b.util.Constants;
import com.qifan.alb2b.util.DataGridModel;
import com.qifan.alb2b.util.Page;

/**
 * 管理员CURD的Dao
 * @author lj
 *
 */
@Repository
public class AdminInfoDao extends BaseDao{
	private static final Log logger=LogFactory.getLog(AdminInfoDao.class);
	
	/**
	 * 根据条件分页查询管理员信息
	 * @param dgm 分页参数
	 * @param uqp 管理员查询参数
	 * @return Page对象（分页信息&分页数据）
	 */
	public Page query(DataGridModel dgm,AdminInfoDomain uqp){
		logger.debug("查询管理员");
		/* 构建sql */
		StringBuilder sql=new StringBuilder();
		List<Object> params=new ArrayList<Object>();
		sql.append("select * from admininfo where 1=1");
		if(uqp.getAdminName()!=null&&!"".equals(uqp.getAdminName())){
			sql.append(" and adminname like ?");
			params.add("%"+uqp.getAdminName()+"%");
		}
		
		//return super.queryMapByPage(sql.toString(), dgm.getRows(), dgm.getPage(),params);
		return super.queryObjByPage(sql.toString(), dgm.getRows(), dgm.getPage(), params, AdminInfoDomain.class);
	}
	
	public AdminInfoDomain getAdminById(String id){
		String sql="select * from admininfo where adminid=?";
		List<AdminInfoDomain> list= JdbcUtil.queryForList(this, AdminInfoDomain.class, sql, id);
		if(list.size()>0)
			return list.get(0);
		return null;
	}
	
	/**
	 * 增加一个管理员
	 * @param AdminInfoDomain
	 */
	public void add(AdminInfoDomain adminInfoDomain){
		super.addObj(adminInfoDomain);
	}
	
	public void update(AdminInfoDomain adminInfoDomain){
		super.updateObj(adminInfoDomain, "adminId");
	}
	
	/**
	 * 批量删除
	 * @param ids
	 */
	public void delete(String[] ids){
		StringBuilder delSql=new StringBuilder();
		delSql.append("delete from admininfo where adminid in(");
		for(int i=0;i<ids.length-1;i++){
			delSql.append("?,");
		}
		delSql.append("?)");
		this.getJdbcTemplate().update(delSql.toString(), ids);
	}
	/**
	 * 批量删除
	 * @param ids
	 */
	public void updateStatus(String[] ids,String status){
		
		StringBuilder delSql=new StringBuilder();
		delSql.append("update  admininfo set status ='"+status+"' where adminid in(");
		for(int i=0;i<ids.length-1;i++){
			delSql.append("?,");
		}
		delSql.append("?)");
		
		this.getJdbcTemplate().update(delSql.toString(),ids);
	}
	public AdminInfoDomain getAdmin(String adminName,String password){
		String sql="select * from admininfo where adminname=? and adminPwd =?";
		List<AdminInfoDomain> list= JdbcUtil.queryForList(this, AdminInfoDomain.class, sql, new String[]{adminName,password});
		if(list.size()>0)
			return list.get(0);
		return null;
	}
}
